import {defineStore} from "pinia";

const tagsViewStore = defineStore('tagsView', {
  state: () => ({
    visitedViews: [],
    cachedViews: [],
    iframeViews: []
  }),
  getters: {
    getVisitedViews() {
      return this.visitedViews;
    },
    getCachedViews(){
      return this.cachedViews;
    },
    getIframeViews() {
      return this.iframeViews
    }
  },
  actions: {
    addView(view) {
      this.addVisitedView(view);
      this.addCachedView(view);
    },
    addIframeView(view) {
      if (this.iframeViews.some(v => v.path === view.path)) return;
      this.iframeViews.push(
          Object.assign({}, view, {
            title: view.meta.title || 'no-name'
          })
      );
    },
    addVisitedView(view) {
      if (this.visitedViews.some(v => v.path === view.path)) return;
      this.visitedViews.push(
          Object.assign({}, view, {
            title: view.meta.title || 'no-name'
          })
      );
    },
    updateVisitedView(view) {
      if (this.getVisitedViews.some(v => v.path === view.path)) return;
      this.visitedViews.unshift(
          Object.assign({}, view, {
            title: view.meta.title || 'no-name'
          })
      );
    },
    addCachedView(view) {
      if (this.getCachedViews.includes(view.name)) return;
      if (!view.meta.noCache) {
        this.cachedViews.push(view.name);
      }
    },
    delView(view) {
      return new Promise(resolve => {
        this.delVisitedView(view);
        this.delCachedView(view);
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        });
      });
    },
    delVisitedView(view) {
      return new Promise(resolve => {
        for (const [i, v] of this.visitedViews.entries()) {
          if (v.path === view.path) {
            this.visitedViews.splice(i, 1);
            break;
          }
        }
        resolve([...this.visitedViews]);
      });
    },
    delCachedView(view) {
      return new Promise(resolve => {
        const index = this.cachedViews.indexOf(view.name);
        index > -1 && this.cachedViews.splice(index, 1);
        resolve([...this.cachedViews]);
      });
    },
    delIframeView(view) {
      return new Promise(resolve => {
        this.iframeViews = this.getIframeViews.filter(
            item => item.path !== view.path
        );
        resolve([...this.iframeViews]);
      });
    },
    delLeftViews(view) {
      return new Promise(resolve => {
        const index = this.getVisitedViews.findIndex(v => v.path === view.path);
        if (index === -1) {
          return;
        }
        this.visitedViews = this.getVisitedViews.filter((item, idx) => {
          if (idx >= index || (item.meta && item.meta.affix)) {
            return true;
          }
          const i = this.getVisitedViews.indexOf(item.name);
          if (i > -1) {
            this.cachedViews.splice(i, 1);
          }
          if (item.meta.link) {
            const fi = this.getIframeViews.findIndex(v => v.path === item.path);
            this.iframeViews.splice(fi, 1);
          }
          return false;
        });
        resolve([...this.visitedViews]);
      });
    },
    delRightViews(view) {
      return new Promise(resolve => {
        const index = this.getVisitedViews.findIndex(v => v.path === view.path);
        if (index === -1) {
          return;
        }
        this.visitedViews = this.getVisitedViews.filter((item, idx) => {
          if (idx <= index || (item.meta && item.meta.affix)) {
            return true;
          }
          const i = this.getCachedViews.indexOf(item.name);
          if (i > -1) {
            this.cachedViews.splice(i, 1);
          }
          if (item.meta.link) {
            const fi = this.getIframeViews.findIndex(v => v.path === item.path);
            this.iframeViews.splice(fi, 1);
          }
          return false;
        });
        resolve([...this.visitedViews]);
      });
    },
    delOthersView(view) {
      return new Promise(resolve => {
        this.delOthersVisitedViews(view).then();
        this.delOthersCachedViews(view).then();
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        });
      });
    },
    delOthersVisitedViews(view) {
      return new Promise(resolve => {
        this.visitedViews = this.getVisitedViews.filter(v => {
          return v.meta.affix || v.path === view.path;
        });
        this.iframeViews = this.getIframeViews.filter(
            item => item.path === view.path
        );
        resolve([...this.visitedViews]);
      });
    },
    delOthersCachedViews(view) {
      return new Promise(resolve => {
        const index = this.getCachedViews.indexOf(view.name);
        if (index > -1) {
          this.cachedViews = this.getCachedViews.splice(index, index + 1);
        } else {
          this.cachedViews = [];
        }
        resolve([...this.cachedViews]);
      });
    },
    delAllViews() {
      return new Promise(resolve => {
        this.delAllVisitedViews().then();
        this.delAllCachedViews().then();
        resolve({
          visitedViews: [...this.visitedViews],
          cachedViews: [...this.cachedViews]
        });
      });
    },
    delAllVisitedViews() {
      return new Promise(resolve => {
        this.visitedViews = this.visitedViews.filter(tag => tag.meta.affix);
        this.iframeViews = [];
        resolve([...this.visitedViews]);
      });
    },
    delAllCachedViews() {
      return new Promise(resolve => {
        this.cachedViews = [];
        resolve([...this.cachedViews]);
      });
    }
  }
});

export default tagsViewStore;